package com.cc2019.buke.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cc2019.buke.R;
import com.cc2019.buke.home.entry.Product;
import com.cc2019.buke.home.entry.Product5;

import java.util.List;


public class ScAdapter extends RecyclerView.Adapter {

    List<Product5> pro_list;
    Context context;

    public ScAdapter(Context context, List<Product5> pro_list){
        this.pro_list = pro_list;
        this.context = context;
    }

    class ChannelHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView textView;
        TextView textView2;
        //对当前的itemView绑定监听事件
        View item_view;

        public ChannelHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.sc_img);
            textView = itemView.findViewById(R.id.sc_pname1);
            textView2 = itemView.findViewById(R.id.sc_pname2);
            //对当前的itemView绑定监听事件
            item_view = itemView;
        }
        public void setData(Product5 product){
            imageView.setImageResource(product.getImgId());
            textView.setText(product.getName());
            textView2.setText(product.getName2());
            item_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Toast.makeText(context,"当前商品被点击,商品价格是："+priceView.getText(),Toast.LENGTH_SHORT).show();
                    onCommProClick.onItemClick(product);
                }
            });
        }

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.home_sc_layout,parent,false);
        return new ChannelHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView. ViewHolder holder, int position) {
        Product5 product = pro_list.get(position);
        ChannelHolder channelHolder = (ChannelHolder)holder;
        channelHolder.setData(product);

    }

    @Override
    public int getItemCount() {
        return pro_list.size();
    }
    //事件处理
    public interface OnCommProClick{
        public void onItemClick(Product5 product);
    }
    public OnCommProClick onCommProClick;

    public void setOnCommProClick(OnCommProClick onCommProClick){
        this.onCommProClick = onCommProClick;
    }
}
